package com.octopus.yangj.customized.service;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.octopus.kernal.controller.AbstractSearchCommand;
import com.octopus.yangj.generated.controller.SearchEventCommand;
import com.octopus.yangj.generated.controller.SearchStatisticsCommand;
import com.octopus.yangj.generated.model.EventStruct;
import com.octopus.yangj.generated.model.StatisticsStruct;
import com.octopus.yangj.generated.service.StatisticsService;

@Transactional
@Service("statisticsService")
public class CustomizedStatisticsService extends StatisticsService{
	
    @Autowired
	private CustomizedEventService eventService;
	
	@Override
	public void createEntity(StatisticsStruct entity){
	}
	
	@Override
	public StatisticsStruct getEntity(Long id){
		return null;
	}
	
	@Override
	public void updateEntity(StatisticsStruct entity){
	}
	
	@Override
	public void deleteEntity(StatisticsStruct entity){
	}
	
	@Override
	public void deleteEntity(Long id){
	}
	
	@Override
	public List<StatisticsStruct> getEntitiesAll(){
		return null;
	}
	
	@Override
	public List<StatisticsStruct> getEntitiesPaged(int offset, int length){
		return null;
	}
	
	@Override
	public List<StatisticsStruct> getEntitiesSortedAll(String[] propertyNames){
		return null;
	}
	
	@Override
	public List<StatisticsStruct> getEntitiesSortedPaged(String[] propertyNames, int offset, int length){
		return null;
	}
	
	@Override
	public int searchCount(AbstractSearchCommand<StatisticsStruct> command){
		return -1;
	}
	
	public List<StatisticsStruct> search(SearchEventCommand eventSearch){
		List<StatisticsStruct> list = new ArrayList<StatisticsStruct>();
		StatisticsStruct statistics = new StatisticsStruct();
		list.add(statistics);
		statistics.setFirstCategoryId(eventSearch.getEntity().getFirstCategoryId());
		statistics.setSecondCategoryId(eventSearch.getEntity().getSecondCategoryId());
		statistics.setThirdCategoryId(eventSearch.getEntity().getThirdCategoryId());
		statistics.setStatus(eventSearch.getEntity().getStatus());
		statistics.setMark(eventSearch.getEntity().getMark());
		int count = eventService.searchCount(eventSearch);
		if(eventSearch.getOperator() != null){
			eventSearch.getOperator().setFirstCategoryId(null);
			eventSearch.getOperator().setSecondCategoryId(null);
			eventSearch.getOperator().setThirdCategoryId(null);
			eventSearch.getOperator().setStatus(null);
			eventSearch.getOperator().setMark(null);
		}
		int total = eventService.searchCount(eventSearch);
		if(total <= 0)
			return null;
		double percent = Double.valueOf(count)/Double.valueOf(total);
		NumberFormat formatter = NumberFormat.getNumberInstance();
		formatter.setMinimumFractionDigits(4);
		formatter.setMaximumFractionDigits(4);
		statistics.setCount(new Long(count));
		statistics.setPercent(Double.valueOf(formatter.format(percent)) * 100);
		statistics.setId(0l);
		return list;
	}
	
	@Override
	public List<StatisticsStruct> search(AbstractSearchCommand<StatisticsStruct> command){
		SearchStatisticsCommand statSearch = (SearchStatisticsCommand)command;
		SearchEventCommand eventSearch = new SearchEventCommand();
		eventSearch.setEntity(new EventStruct());
		if(statSearch.getOperator() != null)
			eventSearch.setOperator(new EventStruct());
		eventSearch.getEntity().setFirstCategoryId(statSearch.getEntity().getFirstCategoryId());
		eventSearch.getEntity().setSecondCategoryId(statSearch.getEntity().getSecondCategoryId());
		eventSearch.getEntity().setThirdCategoryId(statSearch.getEntity().getThirdCategoryId());
		eventSearch.getEntity().setStatus(statSearch.getEntity().getStatus());
		eventSearch.getEntity().setMark(statSearch.getEntity().getMark());
		if(statSearch.getOperator() != null){
			eventSearch.getOperator().setFirstCategoryId(statSearch.getOperator().getFirstCategoryId());
			eventSearch.getOperator().setSecondCategoryId(statSearch.getOperator().getSecondCategoryId());
			eventSearch.getOperator().setThirdCategoryId(statSearch.getOperator().getThirdCategoryId());
			eventSearch.getOperator().setStatus(statSearch.getOperator().getStatus());
			eventSearch.getOperator().setMark(statSearch.getOperator().getMark());
		}
		return this.search(eventSearch);
	}
	
	@Override
	public List<StatisticsStruct> findEntities(AbstractSearchCommand<StatisticsStruct> command){
		return null;
	}
	
	@Override
	public List<StatisticsStruct> findEntitiesAll(StatisticsStruct entity, boolean like){
		return null;
	}
	
	@Override
	public List<StatisticsStruct> findEntitiesPaged(StatisticsStruct entity, int offset, int length, boolean like){
		return null;
	}
	
	@Override
	public List<StatisticsStruct> findEntitiesSortedAll(StatisticsStruct entity, String[] propertyNames, boolean like){
		return null;
	}
	
	@Override
	public List<StatisticsStruct> findEntitiesSortedPaged(StatisticsStruct entity, String[] propertyNames, int offset, int length, boolean like){
		return null;
	}

}
